package com.ruoyi.taskasses.domain;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        float v = bigDecimal.abs().floatValue();
        if (v >= 1000 || v <= 0.001) {
            DecimalFormat decimalFormat = new DecimalFormat();
            decimalFormat.applyPattern("0.########E0");
            jsonGenerator.writeString(decimalFormat.format(bigDecimal).replace("E0",""));
        }else {
            DecimalFormat decimalFormat = new DecimalFormat();
            decimalFormat.applyPattern("0.########");
            jsonGenerator.writeString(decimalFormat.format(bigDecimal));
        }

    }
}
